package com.javatest.chapter2;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author liuziyang
 * @create 2020-03-29 14:36
 */
public class Sample3 {
    private final static int AVALIABLE_PROCESSORS = Runtime.getRuntime().availableProcessors();
    private final static ThreadPoolExecutor POOL_EXECUTOR = new ThreadPoolExecutor(AVALIABLE_PROCESSORS, AVALIABLE_PROCESSORS * 2, 1, TimeUnit.MINUTES, new LinkedBlockingDeque<>(5), new ThreadPoolExecutor.CallerRunsPolicy());

    public static void main(String[] args) throws InterruptedException {
        final long start = System.currentTimeMillis();
        //1.开启异步单元执行任务a
        POOL_EXECUTOR.execute(WorkExecutor::doSomethingA);
        //2.执行任务b
        WorkExecutor.doSomethingB();
        //3.同步等待线程a运行结束
        System.out.println(System.currentTimeMillis() - start);
        //4.挂起当前线程
        Thread.currentThread().join();
    }
}
